<!-- eslint-disable vue/no-parsing-error -->
<template>
  <div class="choice-container">
    <div class="app-container">
      <Discipline @searchhh="aSearchList" @clearlist="aclearlist"></Discipline>
      <el-tabs v-model="activeName" type="card" @tab-click="handleClick">
        <el-tab-pane label="全部" name="first" ></el-tab-pane>
        <el-tab-pane label="待审核" name="second" ></el-tab-pane>
        <el-tab-pane label="已审核" name="third"></el-tab-pane>
        <el-tab-pane label="已拒绝" name="fourth"></el-tab-pane>
      </el-tabs>
      <!-- 消息提示 -->
      <el-alert
        :title="'数据一共 ' + page.counts + ' 条'"
        show-icon
        type="info"
        :closable="false"
        style="margin-bottom: 15px"
      >
      </el-alert>
      <!-- 表格 -->
      <el-table :data="choiceList" style="width: 100%">
        <el-table-column prop="number" label="试题编号" width="150">
        </el-table-column>
        <el-table-column prop="subject" label="学科" width="120">
        </el-table-column>
        <el-table-column prop="catalog" label="目录" width="120">
        </el-table-column>
        <el-table-column prop="questionType" label="题型" width="100">
          <template slot-scope="{ row }">
            {{ row.questionType > 1 ? "多选" : "单选" }}
          </template>
        </el-table-column>
        <el-table-column prop="question" label="题干" width="300">
          <template slot-scope="{ row }">
            {{ row.question.replace(/<.*?>/gi, "") }}
          </template>
        </el-table-column>
        <el-table-column prop="addDate" label="录入时间" width="180">
          <template slot-scope="{ row }">
            {{ row.addDate.replace(/T|.000Z/g, " ") }}
          </template>
        </el-table-column>
        <el-table-column prop="difficulty" label="难度" width="120">
          <template slot-scope="{ row }">
            {{
              row.difficulty == 1
                ? "简单"
                : row.difficulty == 2
                ? "一般"
                : "困难"
            }}
          </template>
        </el-table-column>
        <el-table-column prop="creator" label="录入人" width="120">
        </el-table-column>
        <el-table-column prop="chkState" label="审核状态" width="120">
          <template slot-scope="{ row }">
            {{
              row.chkState == 0
                ? "待审核"
                : row.chkState == 1
                ? "已审核"
                : "已拒绝"
            }}
          </template>
        </el-table-column>
        <el-table-column prop="chkRemarks" label="审核意见" width="120">
        </el-table-column>
        <el-table-column prop="chkUser" label="审核人" width="120">
        </el-table-column>
        <el-table-column prop="publishState" label="发布状态" width="120">
          <template slot-scope="{ row }">
            {{
              row.publishState == 0
                ? "已下架"
                : row.publishState == 1
                ? "已发布"
                : "待发布"
            }}
          </template>
        </el-table-column>
        <el-table-column align="center" fixed="right" label="操作" width="220">
          <template slot-scope="scope">
            <el-button
              type="text"
              size="small"
              @click="ShowDiaor(scope.$index, scope.row)"
            >
              预览
            </el-button>
            <el-button :disabled="scope.row.chkState == 0?false:true" type="text" size="small" @click="audit(scope.row)">
              审核
            </el-button>
            <el-button
              type="text"
              size="small"
              :disabled="scope.row.publishState == 0?false:true"
              @click="alter(scope.row.id)"
            >
              修改
            </el-button>
            <el-button
              type="text"
              size="small"
              @click="
                modification(scope.row, scope.row.publishState == 0 ? 1 : 0)
              "
            >
              {{ scope.row.publishState == 0 ? "上架" : "下架" }}
            </el-button>
            <el-button :disabled="scope.row.publishState == 1?true:false" @click="deleteRow(scope.row)" type="text" size="small">
              删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 预览 -->
      <questions-mypreview
        :dialogFormVisible.sync="dialogFormVisible"
        :yulandata="yulandata"
      />
      <!-- 分页 -->
      <div class="block">
        <el-pagination
          background
          @size-change="handleSizeChange"
          @current-change="changePage"
          :page-sizes="[5, 10, 20, 50]"
          :page-size="page.pagesize"
          :current-page="page.page"
          layout="prev, pager, next,sizes,jumper"
          :total="page.counts"
          style="margin-top: 20px; text-align: right"
        >
        </el-pagination>
      </div>
      <!-- 审核弹框 -->
      <el-dialog title="题目审核" :visible.sync="dialogVisible" width="20%">
        <div class="radio_two">
          <el-radio v-model.number="auditList.chkState" :label="1">通过</el-radio>
          <el-radio v-model.number="auditList.chkState" :label="2">拒绝</el-radio>
        </div>

        <el-input
          type="textarea"
          v-model="auditList.chkRemarks"
          placeholder="请输入审核意见"
        ></el-input>
        <span slot="footer" class="dialog-footer">
          <el-button @click="dialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="getaudit">确 定</el-button>
        </span>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import Discipline from './../components/questions-discipline.vue'
import { choice, detail, remove, choicePublish, choiceCheck } from '@/api/hmmm/questions'
import QuestionsMypreview from './../components/questions-mypreview.vue'
export default {
  name: 'ChoicesList',
  components: {
    Discipline,
    QuestionsMypreview
  },
  data () {
    return {
      activeName: 'first',
      choiceList: [],
      page: {
        page: 1, // 当前页码
        pagesize: 10, // 请求条数
        counts: 0, // 总数
        pages: null
        // id: null
      },
      data: { id: null, publishState: null },
      dialogFormVisible: false, // 预览
      yulandata: { question: '', answer: '' },
      dialogVisible: false, // 审核弹框

      auditList: {
        id: null,
        chkState: 1,
        chkRemarks: ''
      }

    }
  },
  created () {
    this.getchoiceList()
  },
  methods: {
    // 待审核
    async handleClick () {
      if (this.activeName === 'first') {
        await this.getchoiceList()
      }
      if (this.activeName === 'second') {
        await this.getchoiceList()
        this.choiceList = this.choiceList.filter(value => {
          return value.chkState === 0
        })
        this.page.counts = this.choiceList.length
      }
      if (this.activeName === 'third') {
        await this.getchoiceList()
        this.choiceList = this.choiceList.filter(value => {
          return value.chkState === 1
        })
        this.page.counts = this.choiceList.length
      }
      if (this.activeName === 'fourth') {
        await this.getchoiceList()
        this.choiceList = this.choiceList.filter(value => {
          return value.chkState === 2
        })
        this.page.counts = this.choiceList.length
      }
    },

    // 输入页码改变时
    changePage (newPage) {
      this.page.page = newPage
      this.getchoiceList()
    },
    // 改变请求条数
    handleSizeChange (val) {
      this.page.pagesize = val
      this.getchoiceList()
    },
    // 修改
    alter (id) {
      this.$router.push({
        path: ('/questions/new?id=' + id)
      })
    },

    // 表格删除
    deleteRow (row) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async () => {
          this.data.id = row.id
          await remove(this.data)
          this.getchoiceList()
          this.$message.success('删除成功')
        })
        .catch(() => {
          this.$message.info('已取消删除')
        })
    },
    // 精选题库列表
    async getchoiceList () {
      const dataList = await choice(this.page)
      // console.log(dataList)
      this.choiceList = dataList.data.items
      // console.log(this.choiceList)
      this.page.counts = dataList.data.counts// 获取列表总数
    },
    // 预览按钮
    async ShowDiaor (index, row) {
      this.data.id = row.id
      const a = await detail(this.data)
      this.yulandata = a.data
      this.dialogFormVisible = true
    },
    // 上架下架
    async modification (row, publishState) {
      this.data.id = row.id
      this.data.publishState = publishState
      await choicePublish(this.data)
      this.getchoiceList()
    },
    // 审核
    async getaudit () {
      if (!this.auditList.chkRemarks) {
        return this.$message.warning('请输入审核意见')
      }
      this.dialogVisible = false
      await choiceCheck(this.auditList)
      this.$message.success('操作成功')
      this.auditList = {
        id: null,
        chkState: 1,
        chkRemarks: ''
      }
      this.getchoiceList()
    },
    audit (row) {
      this.auditList.id = row.id
      this.dialogVisible = true
    },
    async aSearchList (val) {
      const {
        data: { items, counts }
      } = await choice(val)
      this.choiceList = items
      this.page.counts = counts
    },
    aclearlist () {
      this.getchoiceList()
    }
  }
}
</script>

<style scoped lang='less'>
.app-container {
  background-color: #fff;
  margin: 10px;
}
.el-button {
  cursor: pointer;
}
.radio_two {
  margin-bottom: 20px;
}
</style>
